/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * xml_nihongo
 * Copyright (C) Massimiliano Maniscalco 2011, 2012 <massi.neptune@yahoo.com>
 * 
 * xml_nihongo is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * xml_nihongo is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _LEZIONE_H_
#define _LEZIONE_H_

#include <glibmm/ustring.h>
#include <list>
#include <algorithm>
#include <iostream>

using namespace std;

class Lezione
{
	
public:
	Lezione ( const Glib::ustring &str = "" ) :
		_nome ( str )
	{
		_parole = new list<Glib::ustring>;
		_kanji = new list<Glib::ustring>;
		_esempi = new list<Glib::ustring>;
	}
	inline Lezione ( const Lezione &lsn );
	inline ~Lezione();
	inline bool operator<( const Lezione &lsn ) const;
	inline bool operator==( const Lezione &lsn ) const;
	inline void set_nome ( const Glib::ustring &str ) { _nome.assign ( str ); }
	inline void set_nome ( const char *cstr ) { _nome.assign ( cstr ); }
	inline Glib::ustring get_nome ( void ) const { return _nome; }
	inline void set_parole ( const list<Glib::ustring> &setStr );
	inline void set_parole ( const Glib::ustring &str );
	inline Glib::ustring get_parole ( void ) const;
	inline const list<Glib::ustring>& get_parole_list ( void ) const { return *_parole; }
	inline bool contiene_parola ( const Glib::ustring &str, bool ricercaEsatta = true ) const { return contiene_stringa ( *_parole, str, ricercaEsatta ); }
	inline void push_parola ( const Glib::ustring &str );
	inline unsigned short count_parole ( void ) const { return _parole->size(); }
	inline void set_kanji ( const list<Glib::ustring> &setStr );
	inline void set_kanji ( const Glib::ustring &str );
	inline Glib::ustring get_kanji ( void ) const;
	inline const list<Glib::ustring>& get_kanji_list ( void ) const { return *_kanji; }
	inline bool contiene_kanji ( const Glib::ustring &str ) const { return contiene_stringa ( *_kanji, str ); }
	inline void push_kanji ( const Glib::ustring &str );
	inline unsigned short count_kanji ( void ) const { return _kanji->size(); }
	inline void set_esempi ( const list<Glib::ustring> &setStr );
	inline void set_esempi ( const Glib::ustring &str );
	inline Glib::ustring get_esempi ( void ) const;
	inline bool contiene_esempio ( const Glib::ustring &str, bool ricercaEsatta = true ) const { return contiene_stringa ( *_esempi, str, ricercaEsatta ); }
	inline void push_esempio ( const Glib::ustring &str );
	inline unsigned short count_esempi ( void ) const { return _esempi->size(); }
	inline void cleanup ( void );
	inline Glib::ustring get_xml_string ( const Glib::ustring &language = "" ) const;

protected:

private:
	Glib::ustring _nome;
	list<Glib::ustring> *_parole;
	list<Glib::ustring> *_kanji;
	list<Glib::ustring> *_esempi;

	void parse_string ( const Glib::ustring &str, list<Glib::ustring> &listStr );
	void create_string ( const list<Glib::ustring> &listStr, Glib::ustring &str ) const;
	inline bool contiene_stringa ( const list<Glib::ustring> &listStr, const Glib::ustring &str, bool ricercaEsatta = true ) const;
};

inline Lezione::
Lezione ( const Lezione &lsn ) :
	_nome ( lsn._nome )
{
	_parole = new list<Glib::ustring> ( *(lsn._parole) );
	_kanji = new list<Glib::ustring> ( *(lsn._kanji) );
	_esempi = new list<Glib::ustring> ( *(lsn._esempi) );
}

inline Lezione::
~Lezione()
{
	delete _parole;
	delete _kanji;
	delete _esempi;
}

inline bool Lezione::
operator<( const Lezione &lsn ) const
{
	return _nome.raw () < lsn._nome.raw ();
}

inline bool Lezione::
operator==( const Lezione &lsn ) const
{
	return _nome.raw () == lsn._nome.raw ();
}

inline void Lezione::
set_parole ( const list<Glib::ustring> &setStr )
{
	_parole->clear ();
	_parole->insert ( _parole->begin(), setStr.begin (), setStr.end () );
}

inline void Lezione::
set_parole ( const Glib::ustring &str )
{
	_parole->clear ();
	parse_string ( str, *_parole );
}

inline Glib::ustring Lezione::
get_parole ( void ) const
{
	Glib::ustring _str = "";
	create_string ( *_parole, _str );
	return _str;
}

inline void Lezione::
push_parola ( const Glib::ustring &str )
{
	if ( !contiene_parola ( str ) )
		_parole->push_back ( str );
}

inline void Lezione::
set_kanji ( const list<Glib::ustring> &setStr )
{
	_kanji->clear ();
	_kanji->insert ( _kanji->begin(), setStr.begin (), setStr.end () );
}

inline void Lezione::
set_kanji ( const Glib::ustring &str )
{
	_kanji->clear ();
	parse_string ( str, *_kanji );
}

inline Glib::ustring Lezione::
get_kanji ( void ) const
{
	Glib::ustring _str = "";
	create_string ( *_kanji, _str );
	return _str;
}

inline void Lezione::
push_kanji ( const Glib::ustring &str )
{
	if ( !contiene_kanji ( str ) )
		_kanji->push_back ( str );
}

inline void Lezione::
set_esempi ( const list<Glib::ustring> &setStr )
{
	_esempi->clear ();
	_esempi->insert ( _esempi->begin(), setStr.begin (), setStr.end () );
}

inline void Lezione::
set_esempi ( const Glib::ustring &str )
{
	_esempi->clear ();
	parse_string ( str, *_esempi );
}

inline Glib::ustring Lezione::
get_esempi ( void ) const
{
	Glib::ustring _str = "";
	create_string ( *_esempi, _str );
	return _str;
}

inline void Lezione::
push_esempio ( const Glib::ustring &str )
{
	if ( !contiene_esempio ( str ) )
		_esempi->push_back ( str );
}

inline bool Lezione::
contiene_stringa ( const list<Glib::ustring> &listStr, const Glib::ustring &str, bool ricercaEsatta ) const
{
	if ( listStr.size() > 0 )
	{
		list<Glib::ustring>::const_iterator _iter = listStr.begin ();
		list<Glib::ustring>::const_iterator _end = listStr.end ();
		while ( _iter != _end )
		{
			if ( ricercaEsatta )
			{
				if ( str.raw() == _iter->raw() )
					return true;
			}
			else
			{
				if ( _iter->raw().find ( str.raw() ) != string::npos )
					return true;
			}
			_iter++;
		}
	}
	return false;
}

inline void Lezione::
cleanup ( void )
{
	set_nome ( "" );
	set_parole ( "" );
	set_kanji ( "" );
	set_esempi ( "" );
}

inline Glib::ustring Lezione::
get_xml_string ( const Glib::ustring &language ) const
{
	Glib::ustring str;
	str.append ( Glib::ustring ( "<lezione>\n" ) );
	str.append ( Glib::ustring ( "\t<nomen>" + _nome + "</nomen>\n" ) );
	str.append ( Glib::ustring ( "\t<verba>" + get_parole () + "</verba>\n" ) );
	str.append ( Glib::ustring ( "\t<notae>" + get_kanji () + "</notae>\n" ) );
	str.append ( Glib::ustring ( "\t<exempla>" + get_esempi () + "</exempla>\n" ) );
	str.append ( Glib::ustring ( "</lezione>\n" ) );
	return str;
}

#endif // _LEZIONE_H_
